package com.bw.exam.controller;

import com.alibaba.fastjson.JSON;
import com.bw.exam.mapper.ItemCatMapper;
import com.bw.exam.pojo.ItemCat;
import com.bw.exam.result.R;
import com.bw.exam.service.ItemCatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: liuzhiping
 * @date: 2024/8/22 14:22
 **/
@RequestMapping("/manage/itemCat")
@RestController
@Slf4j
public class ItemCatController {

    @Autowired
    private ItemCatMapper itemCatMapper;

    @Autowired
    private ItemCatService itemCatService;

    @RequestMapping("tree")
    public R tree(){
        return R.success(itemCatMapper.selectItemTree());
    }


    @RequestMapping("tree1")
    public R tree1(){
        return R.success(this.itemCatService.listTree1());
    }

    @RequestMapping("saveOrUpdate")
    public R add(@RequestBody ItemCat itemCat){
        log.info("商品分类：添加 itemCat:{}", JSON.toJSONString(itemCat));
        this.itemCatService.saveOrUpdate(itemCat);
        return  R.success();
    }

    @RequestMapping("findById/{id}")
    public R findById(@PathVariable Long id){
        log.info("商品分类 查询 id:{}", id);
        return R.success(this.itemCatService.getById(id));
    }


    @RequestMapping("delById/{id}")
    public R delById(@PathVariable Long id){
        log.info("商品分类 删除 id:{}", id);

        this.itemCatService.delById2(id);
        return R.success();
    }

}
